﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Loginaspx : System.Web.UI.Page
{
   // User userSrv = new User();
    MyDataDataContext db = new MyDataDataContext();
    

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            //NewUser.aspx页面传递过来的查询字符串变量name值非空
            if(Request.QueryString["name"]!=null)
            {
                txtName.Text = Request.QueryString["name"];
                lblMsg.Text = "注册成功，请登录！";
            }
        }
    }
    protected void BtnLogin_Click(object sender, EventArgs e)
    {
        User user = (from c in db.User
                    where c.UserNickname == txtName.Text && c.UserPwd == txtPwd.Text
                    select c).FirstOrDefault();

        Manager manager = (from c in db.Manager
                     where c.ManagerName == txtName.Text && c.ManagerPwd== txtPwd.Text
                     select c).FirstOrDefault();

        if (user!=null)//一般用户登录
        {
            Session.Clear();
            Session["UserId"] = user.UserID;
            Session["UserName"] = txtName.Text;
            Response.Redirect("~/Default.aspx");
        }
        else if (manager != null)//管理员登录
        {
            Session.Clear();
            Session["AdminId"] = manager.ManagerID;
            Session["AdminName"] = txtName.Text;
            Response.Redirect("~/admin/Administrator.aspx");
        }
        else
        {
            lblMsg.Text = "用户名或密码错误！";
        }

        bool IsChonfu = false;
        if (this.txtName.Text.Trim().Length < 1)
        {
            lblMsg.Visible = true;
        }
        else
        {
            string UserIp = Request.UserHostAddress.ToString();//获取服务器ip地址        
            string UserInfo = this.txtName.Text.Trim() + "--" + UserIp;
            ArrayList UserList;

            if (Application["UserList"] == null)
            {
                UserList = new ArrayList();
                UserList.Add(UserInfo);
                Application["UserList"] = UserList;                
            }
            else
            {

                UserList = (ArrayList)Application["UserList"];

                for (int i = 0; i < UserList.Count; i++)
                {
                    if (UserInfo == UserList[i].ToString())
                    {
                        IsChonfu = true;
                    }
                    else
                    {
                        IsChonfu = false;
                    }
                }
                if (IsChonfu == false)
                {
                    UserList.Add(UserInfo);
                }
                Application["UserList"] = UserList;                
            }
        }

    }

}